﻿;/////////////////////////////////////////////////////////////;
;                                                             ;
;           SimpleDX11 Demo 3_1 (Lighting and Sprites)        ;
;                                                             ;
;                   (c) 2013 SimpleDX Team                    ;
;                                                             ;
;                         License: MIT                        ;
;                                                             ;    
;/////////////////////////////////////////////////////////////;

; Initialize the PB Spritesystem and use the DirectX11 subsystem
InitSprite()

;Include the SimpleDX11 main file (change the pathes in there before using it)
XIncludeFile "../../SimpleDX11.pbi"


; Initialize Keyboard
If Not InitKeyboard()
  MessageRequester("Error", "Could not initialize the keyboard!")
  End  
EndIf


; Open a Window
#Window = 0
If OpenWindow(#Window, 50, 200, 800, 600,"SimpleDX11 initialization",#PB_Window_SystemMenu | #PB_Window_SizeGadget)
  If OpenWindowedScreen(WindowID(#Window), 0, 0, WindowWidth(#Window), WindowHeight(#Window))  
      CreateSprite(0, 50, 50)
      If StartDrawing(SpriteOutput(0))
        Box(0, 0, 50, 50, RGB(255, 0, 155))
        Box(5, 5, 40, 40, RGB(155, 0, 255))
        StopDrawing()
      EndIf    
  Else  
    MessageRequester("Error", "Could not open the Screen!")
    End    
  EndIf  
Else  
  MessageRequester("Error", "Could not open the Window!")
  End
EndIf

; Must be used after OpenScreen / OpenWindowedScreen
UseModule SimpleDX11

Define *Screen3D.Screen3D = New_Screen3D()


; Add a camera
Define *Camera.Camera = New_Camera(*Screen3D) 
*Camera\SetPosition(0.0,0.0,-9.0)

; Add a light
*Light.Light = New_Light() 
*Light\SetDirection(1.0, 0.0, 0.0)
*Light\SetDiffuseColor($FF48D33F)
*Light\SetAmbientColor($FF48473F)
*Light\SetSpecularColor($FF48473F)

; Add a model
*Model.Model = New_Model(*Screen3D, #SDX_Vertex_Normals | #SDX_Vertex_UVMap)
*Texture = *Model\LoadTexture("..\Models\textures\tex_1.dds")
*Model\AddCube(1.0) 
*Model\SetModel()


*vShader.Shader = New_Shader(*Screen3D, #VertexShader)
If *vShader\CompileShaderFile("Shader\Vertex.vs", "VShader", "vs_5_0") 
   *vShader\Release() : *Model\Release() : *Camera\Release() : *Screen3D\Release() : End
  End 
EndIf

*pShader.Shader = New_Shader(*Screen3D, #PixelShader)
*pShader\AddTexture(*Texture)
*pShader\AddLighting()
If *pShader\CompileShaderFile("Shader\Pixel.ps", "PShader", "ps_5_0")    
  *pShader\Release() : *Model\Release() : *Camera\Release() : *Screen3D\Release() : End
  End 
EndIf

Define Rotation.f, direction.i = 2, x.i, y.i

Define ShaderDataDesc.Shader_DataDescription
ShaderDataDesc\Camera = *Camera
ShaderDataDesc\Light  = *Light
CopyMemory(*Screen3D\GetWorldMatrix(), ShaderDataDesc\WorldMatrix, SizeOf(D3DMATRIX))
CopyMemory(*Screen3D\GetProjectionMatrix(), ShaderDataDesc\ProjectionMatrix, SizeOf(D3DMATRIX))


Repeat  
  ExamineKeyboard()
  Event.l = WindowEvent()  
     
  ClearScreen($799FFF)  

  DisplaySprite(0, -y + 500 , -y +400)
  y + direction
  If y > 240 : direction = -2 : EndIf
  If y < 0   : direction =  2 : EndIf
  
  
  *Screen3D\Start3D()
    *Camera\Render()

    Rotation + (#PI * 0.005)
    If Rotation >= 360.0
      Rotation = 0.005
    EndIf   
    D3DXMatrixRotationY(ShaderDataDesc\WorldMatrix, Rotation) 
  
    ; Set the shader data
    *vShader\SetShaderData(ShaderDataDesc)
    *pShader\SetShaderData(ShaderDataDesc)
    
    ; Render all the shader and vertexbuffer ....
   	*vShader\Render()
   	*pShader\Render()   
   	*Model\Render()  
   	
  *Screen3D\Stop3D()
  
  DisplaySprite(0, x + 300, x + 150)
  x + direction
  If x > 240 : direction = -2 : EndIf
  If x < 0   : direction =  2 : EndIf  
  
  FlipBuffers()
  
  Delay(1)
Until Event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)

; Release SimpleDX11 from Memory
; Do not forget this!
*Light\Release()
*Camera\Release()
*vShader\Release()
*pShader\Release()
*Screen3D\Release()
UnuseModule SimpleDX11

CloseScreen()



; IDE Options = PureBasic 5.20 beta 14 LTS (Windows - x64)
; CursorPosition = 2
; EnableUnicode
; EnableXP
; SubSystem = DirectX11